package org.quasar.airNova.presentationLayer;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import org.quasar.airNova.businessLayer.Sexo;

public class CriarIndividual extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField nContribuinteJTF;
	private JTextField moradaJTF;
	private JTextField nomeJTF;
	private JTextField nPassaporteJTF;
	private JTextField emailJTF;
	private Gui gui;
	private JTextField nTelemovelJTF;
	private JComboBox<Object> comboBox;
	
	/**
	 * Create the panel.
	 */
	public CriarIndividual(Gui gui) {
		setGui(gui);
		setLayout(new BorderLayout(0, 0));
		this.setSize(500, 350);
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("NIF");
		lblNewLabel.setBounds(40, 70, 151, 14);
		panel.add(lblNewLabel);
		
		nContribuinteJTF = new JTextField();
		nContribuinteJTF.setBounds(231, 67, 203, 20);
		panel.add(nContribuinteJTF);
		nContribuinteJTF.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Morada");
		lblNewLabel_2.setBounds(40, 98, 126, 14);
		panel.add(lblNewLabel_2);
		
		moradaJTF = new JTextField();
		moradaJTF.setBounds(231, 95, 203, 20);
		panel.add(moradaJTF);
		moradaJTF.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Nome");
		lblNewLabel_1.setBounds(40, 123, 126, 14);
		panel.add(lblNewLabel_1);
		
		nomeJTF = new JTextField();
		nomeJTF.setBounds(231, 120, 203, 20);
		panel.add(nomeJTF);
		nomeJTF.setColumns(10);
		
		JLabel lblNmeroCartaoDe = new JLabel("N\u00FAmero de Passaporte");
		lblNmeroCartaoDe.setBounds(40, 148, 126, 14);
		panel.add(lblNmeroCartaoDe);
		
		nPassaporteJTF = new JTextField();
		nPassaporteJTF.setBounds(231, 145, 203, 20);
		panel.add(nPassaporteJTF);
		nPassaporteJTF.setColumns(10);
		
		JLabel lblLugar = new JLabel("Email");
		lblLugar.setBounds(40, 173, 126, 14);
		panel.add(lblLugar);
		
		emailJTF = new JTextField();
		emailJTF.setBounds(231, 170, 203, 20);
		panel.add(emailJTF);
		emailJTF.setColumns(10);
		
		JButton btnNewButton_2 = new JButton("Apagar Campos");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				apagarCampos();
			}

			
		});
		btnNewButton_2.setBounds(249, 248, 126, 23);
		panel.add(btnNewButton_2);
		
		JButton btnNewButton = new JButton("Submeter");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getGui().getDatabase();
				Sexo newSexo;
				if(comboBox.getSelectedIndex() == 0){
					newSexo = Sexo.masculino;
				}
				else newSexo = Sexo.feminino;
					AirNova_Database.criarIndividual(0, nomeJTF.getText(), moradaJTF.getText(), nContribuinteJTF.getText(), nPassaporteJTF.getText(), emailJTF.getText(), newSexo, nTelemovelJTF.getText());
				apagarCampos();
			}
		});
		btnNewButton.setBounds(40, 248, 98, 23);
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Fechar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getGui().getTabbedPane().removeTabAt(getGui().getTabbedPane().getSelectedIndex());
			}
		});
		btnNewButton_1.setBounds(150, 248, 89, 23);
		panel.add(btnNewButton_1);
		
		JLabel lblNewLabel_3 = new JLabel("Criar Novo Individual");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblNewLabel_3.setBounds(30, 24, 161, 32);
		panel.add(lblNewLabel_3);
		
		nTelemovelJTF = new JTextField();
		nTelemovelJTF.setColumns(10);
		nTelemovelJTF.setBounds(231, 195, 203, 20);
		panel.add(nTelemovelJTF);
		
		JLabel lblTelemvel = new JLabel("Telem\u00F3vel");
		lblTelemvel.setBounds(40, 198, 126, 14);
		panel.add(lblTelemvel);
		
		JLabel lblSexo = new JLabel("Sexo");
		lblSexo.setBounds(40, 223, 126, 14);
		panel.add(lblSexo);
		
		comboBox = new JComboBox<Object>(Tools.getSexo());
		comboBox.setBounds(231, 220, 203, 20);
		panel.add(comboBox);

	}

	public Gui getGui() {
		return gui;
	}

	public void setGui(Gui gui) {
		this.gui = gui;
	}

	private void apagarCampos() {
		nomeJTF.setText("");
		moradaJTF.setText("");
		nContribuinteJTF.setText("");
		nPassaporteJTF.setText("");
		emailJTF.setText("");
		nTelemovelJTF.setText("");
	}
}
